package mysql

import (
	"cloud-storage/global"
	"cloud-storage/utils"
	"path"
	"strconv"
	"strings"
	"time"
)

//文件表
type MyFile struct {
	Id             int
	FileName       string //文件名
	FileHash       string //文件哈希值
	FileStoreId    int    //文件仓库id
	FilePath       string //文件存储路径
	DownloadNum    int    //下载次数
	UploadTime     string //上传时间
	ParentFolderId int    //父文件夹id
	Size           int64  //文件大小
	SizeStr        string //文件大小单位
	Type           int    //文件类型
	Postfix        string //文件后缀
}

// CreateFile 添加文件数据
func CreateFile(filename, fileHash string, fileSize int64, fId string, fileStoreId int) error {
	var sizeStr string
	//获取文件后缀
	fileSuffix := path.Ext(filename)
	//获取文件名
	filePrefix := filename[0 : len(filename)-len(fileSuffix)]
	fid, _ := strconv.Atoi(fId)

	if fileSize < 1048576 {
		sizeStr = strconv.FormatInt(fileSize/1024, 10) + "KB"
	} else {
		sizeStr = strconv.FormatInt(fileSize/102400, 10) + "MB"
	}

	myFile := MyFile{
		FileName:       filePrefix,
		FileHash:       fileHash,
		FileStoreId:    fileStoreId,
		FilePath:       "",
		DownloadNum:    0,
		UploadTime:     time.Now().Format("2006-01-02 15:04:05"),
		ParentFolderId: fid,
		Size:           fileSize / 1024,
		SizeStr:        sizeStr,
		Type:           utils.GetFileTypeInt(fileSuffix),
		Postfix:        strings.ToLower(fileSuffix),
	}
	return global.DB.Create(&myFile).Error
}

// CurrFileExists 判断当前文件夹是否有同名文件
func CurrFileExists(fId, filename string) bool {
	var file MyFile
	//获取文件后缀
	fileSuffix := strings.ToLower(path.Ext(filename))
	//获取文件名
	filePrefix := filename[0 : len(filename)-len(fileSuffix)]
	global.DB.Where("parent_folder_id = ? and file_name = ? and postfix = ?", fId, filePrefix, fileSuffix).First(&file)
	if file.Size > 0 {
		return false
	}
	return true
}

// FileOssExists 通过hash判断文件是否已上传过oss
func FileOssExists(fileHash string) bool {
	var file MyFile
	global.DB.Find(&file, "file_hash = ?", fileHash)
	if file.FileHash != "" {
		return false
	}
	return true
}

// SubtractSize 文件上传成功减去相应容量
func SubtractSize(size int64, fileStoreId int) error {
	var fileStore FileStore
	err := global.DB.Where("user_id = ?", fileStoreId).First(&fileStore).Error
	fileStore.CurrentSize = fileStore.CurrentSize + size/1024
	fileStore.MaxSize = fileStore.MaxSize - size/1024
	err = global.DB.Save(&fileStore).Error
	return err
}

// GetFileInfo 通过文件id获取文件信息
func GetFileInfo(fId string) (file MyFile, err error) {
	err = global.DB.Where("id = ?", fId).First(&file).Error
	return
}

// DownloadNumAdd 下载次数
func DownloadNumAdd(fId string) error {
	var file MyFile
	err := global.DB.First(&file, fId).Error
	file.DownloadNum = file.DownloadNum + 1
	err = global.DB.Save(&file).Error
	return err
}

// DeleteFileOss 删除文件
func DeleteFileOss(fId string) error {
	var file MyFile
	err := global.DB.Where("id = ?", fId).Delete(&file).Error
	return err
}

// GetUserFileCount 获取文件数量
func GetUserFileCount(fileStoreId int) (count int64, err error) {
	err = global.DB.Model(&MyFile{}).Where("file_store_id = ?", fileStoreId).Count(&count).Error
	return
}

// GetFileDetailUser 获取文件的使用明细情况
func GetFileDetailUser(fileStoreId int) map[string]int64 {
	fileDetailUserMap := make(map[string]int64, 0)
	//文档类型
	docCount := global.DB.Model(MyFile{}).Where("file_store_id = ? AND type = ?", fileStoreId, 1).RowsAffected
	fileDetailUserMap["docCount"] = docCount
	//图片类型
	imgCount := global.DB.Model(MyFile{}).Where("file_store_id = ? AND type = ?", fileStoreId, 2).RowsAffected
	fileDetailUserMap["imgCount"] = imgCount
	//视频类型
	videoCount := global.DB.Model(MyFile{}).Where("file_store_id = ? AND type = ?", fileStoreId, 3).RowsAffected
	fileDetailUserMap["videoCount"] = videoCount
	//音乐类型
	musicCount := global.DB.Model(MyFile{}).Where("file_store_id = ? AND type = ?", fileStoreId, 4).RowsAffected
	fileDetailUserMap["musicCount"] = musicCount
	//其他类型
	otherCount := global.DB.Model(MyFile{}).Where("file_store_id = ? AND type = ?", fileStoreId, 5).RowsAffected
	fileDetailUserMap["otherCount"] = otherCount

	return fileDetailUserMap
}

// GetUserFile 获取用户的文件
func GetUserFile(parentId string, storeId int) (files []MyFile, err error) {
	err = global.DB.Find(&files, "file_store_id = ? and parent_folder_id = ?", storeId, parentId).Error
	return
}

// GetTypeFile 根据文件类型获取文件
func GetTypeFile(fileType, fileStoreId int) (files []MyFile, err error) {
	err = global.DB.Find(&files, "file_store_id = ? and type = ?", fileStoreId, fileType).Error
	return
}
